package com.jade.bss.ec.ware.impl;

import java.util.List;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.jade.bss.ec.ware.WareAttrDao;
import com.jade.bss.ec.ware.WareAttrGroup;
import com.jade.bss.ec.ware.WareAttribute;
import com.jade.bss.ec.ware.WareException;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;

/**
 * 采用JPA方式实现的数据访问接口。
 */
@Named ("bss_wareAttrDao")
public class JpaAttrDao
        implements WareAttrDao
{
    protected static final String SQL_GROUP = "SELECT attrGroup FROM com.jade.bss.ec.ware.WareAttrGroup attrGroup";
    protected static final String SQL_ATTR = "SELECT wareAttr FROM com.jade.bss.ec.ware.WareAttr wareAttr";
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    @Caching (evict = {@CacheEvict (value = "bss.ec.ware.attrgrp.g", key = "#group.id"),
            @CacheEvict (value = "bss.ec.ware.attrgrp.l", allEntries = true),
            @CacheEvict (value = "bss.ec.ware.attrgrp.q", allEntries = true)})
    public void addGroup(WareAttrGroup group)
            throws WareException
    {
        entityManager.persist(group);
    }

    @Caching (evict = {@CacheEvict (value = "bss.ec.ware.attrgrp.g", key = "#group.id"),
            @CacheEvict (value = "bss.ec.ware.attrgrp.l", allEntries = true),
            @CacheEvict (value = "bss.ec.ware.attrgrp.q", allEntries = true)})
    public void updateGroup(WareAttrGroup group)
            throws WareException
    {
        entityManager.merge(group);
    }

    @Caching (evict = {@CacheEvict (value = "bss.ec.ware.attrgrp.g", key = "#group.id"),
            @CacheEvict (value = "bss.ec.ware.attrgrp.l", allEntries = true),
            @CacheEvict (value = "bss.ec.ware.attrgrp.q", allEntries = true)})
    public void removeGroup(WareAttrGroup group)
            throws WareException
    {
        WareAttrGroup old = getGroup(group.getId());
        if (old != null) {
            entityManager.remove(old);
        }
    }

    @Cacheable (value = "bss.ec.ware.attrgrp.g", key = "#id")
    public WareAttrGroup getGroup(long id)
            throws WareException
    {
        return entityManager.find(WareAttrGroup.class, id);
    }

    @Cacheable (value = "bss.ec.ware.attrgrp.l", key = "#criteria")
    public List<WareAttrGroup> listGroup(Criteria criteria)
    {
        return SqlUtils.sqlQueryWithoutCount(SQL_GROUP, entityManager, criteria);
    }

    @Cacheable (value = "bss.ec.ware.attrgrp.q", key = "#criteria")
    public PagingList<WareAttrGroup> queryGroup(Criteria criteria)
    {
        return SqlUtils.sqlQuery(SQL_GROUP, entityManager, criteria);
    }

    @Caching (evict = {@CacheEvict (value = "bss.ec.ware.attg.g", key = "#attribute.id"),
            @CacheEvict (value = "bss.ec.ware.attr.l", allEntries = true),
            @CacheEvict (value = "bss.ec.ware.attr.q", allEntries = true)})
    public void addAttr(WareAttribute attribute)
            throws WareException
    {
        entityManager.persist(attribute);
    }

    @Caching (evict = {@CacheEvict (value = "bss.ec.ware.attr.g", key = "#attribute.id"),
            @CacheEvict (value = "bss.ec.ware.attr.l", allEntries = true),
            @CacheEvict (value = "bss.ec.ware.attr.q", allEntries = true)})
    public void updateAttr(WareAttribute attribute)
            throws WareException
    {
        entityManager.merge(attribute);
    }

    @Caching (evict = {@CacheEvict (value = "bss.ec.ware.attr.g", key = "#attribute.id"),
            @CacheEvict (value = "bss.ec.ware.attr.l", allEntries = true),
            @CacheEvict (value = "bss.ec.ware.attr.q", allEntries = true)})
    public void removeAttr(WareAttribute attribute)
            throws WareException
    {
        WareAttribute old = getAttr(attribute.getId());
        if (old != null) {
            entityManager.remove(old);
        }

    }

    @Cacheable (value = "bss.ec.ware.attr.g", key = "#id")
    public WareAttribute getAttr(long id)
            throws WareException
    {
        return entityManager.find(WareAttribute.class, id);
    }

    @Cacheable (value = "bss.ec.ware.attr.l", key = "#criteria")
    public List<WareAttribute> listAttr(Criteria criteria)
    {
        return SqlUtils.sqlQueryWithoutCount(SQL_ATTR, entityManager, criteria);
    }

    @Cacheable (value = "bss.ec.ware.attr.q", key = "#criteria")
    public PagingList<WareAttribute> queryAttr(Criteria criteria)
    {
        return SqlUtils.sqlQuery(SQL_ATTR, entityManager, criteria);
    }
}
